package com.zhiyou.aifeng.common.helper

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager

class MultiGapDecoration @JvmOverloads constructor(
    horizontal: Int = 10,
    vertical: Int = 10,
    topEnable: Boolean = true,
    bottomEnable: Boolean = true,
    horizontalEnable: Boolean = true
) : RecyclerView.ItemDecoration() {

    private val horizontalSize = dp2px(horizontal.toFloat())
    private val verticalSize = dp2px(vertical.toFloat())
    private val isTopEnabled = topEnable
    private val isBottomEnabled = bottomEnable
    private val isHorizontalEnabled = horizontalEnable

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        val position = parent.getChildLayoutPosition(view)
        val adapterPostion = parent.getChildAdapterPosition(view)

        when (parent.layoutManager) {
            is GridLayoutManager -> {
                val manager = parent.layoutManager as GridLayoutManager
                val itemCount = manager.itemCount                                     //item总数
                val spanCount = manager.spanCount                                     //列数
                val sizeLookup = manager.spanSizeLookup  //item所占列数管理类
                val spanSize = sizeLookup.getSpanSize(adapterPostion)                 //当前item所占列数

                if (spanCount > 0) {
                    if (spanSize == spanCount) {
                        // item占满一行，不做偏移处理
                        outRect.set(0, 0, 0, 0)
                    } else {
                        // 底部第一个占满行的索引
                        val lastFullSpanCountPos = getLastFullSpanCountPostion(sizeLookup, spanCount, adapterPostion)
                        // 当前item是否在最后一列
                        val isLastCol = isLastGridCol(spanCount, position, lastFullSpanCountPos)
                        // 当前item是否在第一行
                        val isFisrtRow = isFristGridRow(spanCount, position, lastFullSpanCountPos)
                        // 当前item是否在最后一行
                        val isLastRow = isLastGridRow(spanCount, position, itemCount)

                        if (spanCount == 2) {
                            // 左右分割方式主要为了让grid当中的item的宽度保持一致
                            outRect.set(
                                if (isLastCol) horizontalSize / 2 else horizontalSize,
                                if (isFisrtRow && isTopEnabled) verticalSize else 0,
                                if (isLastCol) horizontalSize else horizontalSize / 2,
                                if (isLastRow && !isBottomEnabled) 0 else verticalSize
                            )
                        } else if (spanCount > 2) {
                            val leftSize: Int
                            val rightSize: Int

                            when ((position - lastFullSpanCountPos) % (spanCount / spanSize)) {
                                // 最后一列
                                0 -> {
                                    leftSize = horizontalSize / 2
                                    rightSize = horizontalSize
                                }
                                // 第一列
                                1 -> {
                                    leftSize = horizontalSize
                                    rightSize = horizontalSize / 2
                                }
                                else -> {
                                    leftSize = horizontalSize / 2
                                    rightSize = horizontalSize / 2
                                }
                            }

                            outRect.set(
                                leftSize,
                                if (isTopEnabled && isFisrtRow) verticalSize else 0,
                                rightSize,
                                verticalSize
                            )
                        }
                    }
                }
            }
            is LinearLayoutManager -> {
                outRect.set(
                    if (isHorizontalEnabled) horizontalSize else 0,
                    if (isTopEnabled) verticalSize else 0,
                    if (isHorizontalEnabled) horizontalSize else 0,
                    if (isBottomEnabled) verticalSize else 0
                )
            }
            is StaggeredGridLayoutManager -> throw IllegalAccessError("暂不支持瀑布流")
        }
    }

    /**
     * 底部最近一个占据 spanCount 整列的位置
     */
    private fun getLastFullSpanCountPostion(
        sizeLookup: GridLayoutManager.SpanSizeLookup,
        spanCount: Int,
        adapterPostion: Int
    ): Int {
        (adapterPostion downTo 0).forEach {
            if (sizeLookup.getSpanSize(it) == spanCount) return it
        }
        return -1
    }

    /**
     * 是否为最后一列数据
     */
    private fun isLastGridCol(
        spanCount: Int,
        position: Int,
        lastFullSpanPosition: Int
    ) = (position - lastFullSpanPosition) % spanCount == 0

    /**
     * 是否为第一行数据
     */
    private fun isFristGridRow(
        spanCount: Int,
        position: Int,
        lastFullSpanPosition: Int
    ) = position - lastFullSpanPosition <= spanCount

    /**
     * 是否为最后一行数据
     */
    private fun isLastGridRow(
        spanCount: Int,
        position: Int,
        itemCount: Int
    ) = itemCount - position <= spanCount

}
